<?php

namespace app\api\controller;

use app\api\model\Courses;
use app\api\model\Friends;
use app\api\model\Leave;
use app\api\model\ParentStudents;
use app\api\model\Qrcode;
use app\api\model\Relation;
use app\api\model\Shuttle;
use app\api\model\Students;
use app\api\model\Parents as ParentsModel;
use app\api\model\TeacherClasses;
use app\api\model\Teachers;
use app\api\model\Users;
use app\common\controller\Api;
use think\Controller;
use think\Request;

/**
 * 家长接口
 */
class Parents extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    /**
     * 家长请假列表
     */
    public function parentApprove($parent_id)
    {
        $leaves = Leave::getLeavesByParent($parent_id);
        $leaves ? $this->success('请求成功', ['leaves' => $leaves]) : $this->error('没有数据', [], 2);
    }

    /**
     * 接送二维码
     */
    public function qrcode()
    {
        $user_id = $this->auth->getUserId();
        $student_id = $this->_studentId($user_id);
        if (!$student_id) {
            $this->error("您的小孩还未加入园区");
        }
        $content = [
            'student_id' => $student_id,
            'user_id' => $user_id
        ];
        $qrcode = Qrcode::createQrcode(json_encode($content));
        $this->success('请求成功', ['qrcode' => $qrcode]);
    }

    /**
     * 验证二维码
     */
    public function shuttle()
    {
        $student_id = $this->request->post('student_id/a');
        $user_id = $this->request->post('user_id');
        $phone = $this->request->post('phone');
        if (!is_array($student_id) || !$student_id) {
            $this->error("学生ID都不能为空");
        }
        if (!$user_id || empty($phone)) {
            $this->error('接送人ID和老师的手机号都不能为空');
        }
        $student_ids = $this->_studentId($user_id);
        if (array_diff($student_id, $student_ids)) {
            $this->error('学生不匹配');
        }
        $teacher = Teachers::getByPhone($phone);
        if (!$teacher) {
            $this->error('您不是园区的老师');
        }
        $result = Shuttle::getByToday($student_id, $teacher->id, $user_id);
        $result ? $this->success('验证成功') : $this->error('验证失败，请联系管理员');
    }

    private function _studentId($user_id)
    {
        $user = Users::find($user_id);
        if (!$user) {
            $this->error('用户不存在');
        }
        //学生
        $parent = ParentsModel::getByPhone($user->phone);
        if ($parent) {
            $parent_id = $parent->id;
        } else {
            $friend = Friends::getFriendByUser($user->phone);
            if ($friend) {
                $parent_id = $friend->parent_id;
            } else {
                return [];
            }
        }
        $student_ids = ParentStudents::getStudentIds($parent_id);
        return $student_ids;
    }

    /**
     * 考勤记录
     */
    public function record()
    {
        $date = $this->request->get('date');
        $student_id = $this->request->get('student_id');
        if (empty($date) || !$student_id) {
            $this->error("时间和学生id都不能为空");
        }
        $records = Shuttle::getRecords($date, $student_id);
        //上课时间
        $class_id = Students::getClassId($student_id);
        $times = Courses::getByMonth($date, $class_id);
        $records ? $this->success("请求成功", compact('records', 'times')) : $this->error("没有数据");
    }
}
